package per.jdz.unsafe;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 解决方案：
 * 1. List<String> list = new Vector<>();
 * 2. List<String> list = Collections.synchronizedList(new ArrayList<>());
 *
 */
public class ListTest {
    public static void main(String[] args) {
        test();
//        List<String> list = new ArrayList<>();
//        List<String> list = new Vector<>();
//        List<String> list = Collections.synchronizedListaaaa(new ArrayList<>());
        // 写入时复制 cow
        // CopyOnWriteArrayList 比 Vector NB在哪？ 效率更高
        // CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        // for (int i = 0; i < 10; i++) {
        //     // ConcurrentModificationException
        //     new Thread(() -> {
        //         list.add(UUID.randomUUID().toString().substring(0, 5));
        //         System.out.println(list);
        //     }, String.valueOf(i)).start();
        // }
    }

    public static void test() {
        List<String> list1 = new ArrayList<>(); 
        List<String> list2 = new ArrayList<>(); 
        List<String> list3 = new ArrayList<>(); 
        new Thread(() -> {
            list1.add(UUID.randomUUID().toString().substring(0, 5));
            System.out.println(list1);
        }, String.valueOf(1)).start();
        new Thread(() -> {
            list2.add(UUID.randomUUID().toString().substring(0, 5));
            System.out.println(list1);
        }, String.valueOf(2)).start();
        new Thread(() -> {
            list3.add(UUID.randomUUID().toString().substring(0, 5));
            System.out.println(list1);
        }, String.valueOf(3)).start();
    }
}
